package com.xinzhidi.web.task;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xinzhidi.web.dtuusr.service.DtuUsrService;
import com.xinzhidi.web.equipment.domain.Equipment;
import com.xinzhidi.web.equipment.service.EquipmentService;
import com.xinzhidi.web.netty.entity.DeviceInstruction;
import com.xinzhidi.web.netty.service.DeviceInstructionService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时检查设备是否在线
 *
 * @author cx
 */
@Component("checkEquipmentOnlineTask")
public class CheckEquipmentOnlineTask {

    @Autowired
    private DtuUsrService dtuUsrService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private DeviceInstructionService deviceInstructionService;

    public void ryNoParams() {
        this.onlineTask();
    }

    /**
     * 下发消息
     **/
    private String onlineTask() {

        List<Long> dtuList = Lists.newArrayList();
        List<String> addressList = Lists.newArrayList();
        //获取待下发的需重连设备的地址
        List<DeviceInstruction> onlineList = equipmentService.getReconnectAddress();
        if(!CollectionUtils.isEmpty(onlineList)){
            dtuList = onlineList.stream().map(DeviceInstruction::getDtuId).collect(Collectors.toList());
            addressList = onlineList.stream().map(DeviceInstruction::getAddress).collect(Collectors.toList());
        }
        List<Equipment> equipmentList = equipmentService.list(new UpdateWrapper<Equipment>()
                .in(!CollectionUtils.isEmpty(dtuList) ,"dtu_id", dtuList)
                .in(!CollectionUtils.isEmpty(addressList) ,"address", addressList)
                .eq("del_flag", 0)
                .eq("status", 2));

        if(CollectionUtils.isEmpty(equipmentList)) {
            return null;
        }
//        deviceInstructionService.assembleMessage(equipmentList , 3 , AgreementConstants.InstructBusinessType.OTHER);
        return null;
    }
}
